//
//  CarEspeciallybuyVC.m
//  New_Runway
//
//  Created by mack on 2018/1/26.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarEspeciallybuyVC.h"
#import "CarSetListCell.h"
#import "DescribeCell.h"
#import "CarBaseInfoModel.h"


@interface CarEspeciallybuyVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSString *desStr;
@property (nonatomic, strong) NSString *money;
@property (nonatomic, strong) CarBaseInfoModel *infomodel;
@property (nonatomic, assign) NSInteger isBuy;


@end

@implementation CarEspeciallybuyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"车辆特卖";
    self.view.backgroundColor = kColorWhite();
    [self.navigationBarView.rightItem setTitle:@"确定" forState:UIControlStateNormal];
    self.infomodel = self.parems[@"info"];
    _money = @"";
    _isBuy = 0;
    if (self.infomodel.result.carSellingPointDesc) {
        self.desStr = self.infomodel.result.carSellingPointDesc;
    }else{
  
        self.desStr = @"";
    }
    if (self.infomodel.result.carBargainPriceState == 1) {
        _money = [NSString stringWithFormat:@"%.2f",self.infomodel.result.carRetailPrice];
        _isBuy = 1;
    }
    [self initTableview];

    
}

-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = MAIN_COLOR();
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableview registerClass:[CarSetListCell class] forCellReuseIdentifier:@"CarSetListCell"];
    
    [self.tableview registerClass:[DescribeCell class] forCellReuseIdentifier:@"DescribeCell"];
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_equalTo(self.navH);
        make.bottom.equalTo(weakSelf.view).mas_equalTo(-self.BotH);
        make.left.right.equalTo(weakSelf.view);
    }];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 40;
    }
    return 100;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        
        CarSetListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarSetListCell"];
        [cell.centerTF mas_updateConstraints:^(MASConstraintMaker *make) {
      
            make.left.equalTo(cell.contentView).mas_offset(115);
        }];
        cell.label.text  = @"特卖价格（万元）";
        cell.centerTF.tag = indexPath.row;
        cell.centerTF.placeholder = @"请填写特卖金额";
        cell.centerTF.text = _money;
        cell.centerTF.delegate = self;
        cell.centerTF.keyboardType = UIKeyboardTypeDecimalPad;
        cell.switchButton.hidden = YES;
        cell.centerLabel.hidden = YES;
        cell.rightImage.hidden = YES;
        cell.centerTF.hidden = NO;
        cell.centerTF.hidden = NO;
        cell.rightImage.hidden = YES;
        return cell;

    }
   
    DescribeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DescribeCell"];
    if ([self.desStr isEqualToString:@""]) {
        self.desStr = @"请输入特卖描述";
    }
    cell.textview.text = self.desStr;
    kSelfWeak;
    cell.returnValueBlock = ^(NSString *strValue) {
        weakSelf.desStr = strValue;
    };
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];//关闭键盘

    
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if ([text returnRealWithFloatLength:2] == YES) {
        _money = text;
    }
    return [text returnRealWithFloatLength:2];
}
-(void)rightItemClick{
    
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];//关闭键盘
    //提交信息
    NSLog(@"%@",self.desStr);
    if ([self.desStr isEqualToString:@"请输入特卖描述"]) {
        self.desStr = @"";
    }
  
    [self submitMessage];
    
}

-(void)submitMessage{
    
    if(![_money isEqualToString:@""]) {
        _isBuy = 1;
    }
    
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,updataCarApi];
    NSDictionary *params = @{@"carRetailPrice":_money,
                             @"carBargainPriceState":@(_isBuy),
                             @"carSellingPointDesc":self.desStr,
                             @"carId":self.infomodel.result.carId
                             };
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        if ([response[@"code"] isEqual:@1]) {
            [ShowMessage showHintMessage:@"特卖价格修改成功" duration:2.0 withRemindId:remindSuccess];
            NSNotification *notificatiom = [NSNotification notificationWithName:@"setPrice" object:self.indexPath userInfo:@{@"type":@"carBargainPriceState",@"value":_money}];
            [[NSNotificationCenter defaultCenter] postNotification:notificatiom];
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            
            [ShowMessage showHintMessage:response[@"msg"] duration:2.0 withRemindId:remindFail];

        }
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];

    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
